<?php


namespace app\api\controller;
/**
 * ============================================================================
 * DSKMS多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 虚拟控制器
 */
class  Sellervrorder extends MobileSeller {

    public function initialize() {
        parent::initialize(); // TODO: Change the autogenerated stub
    }
    
    /**
     * @api {POST} api/Sellervrorder/order_list 虚拟订单列表
     * @apiVersion 1.0.0
     * @apiGroup Sellervrorder
     *
     * @apiHeader {String} X-DS-KEY 卖家授权token
     *
     * @apiParam {String} order_sn 订单号
     * @apiParam {Int} skip_off 非取消状态 1是
     * @apiParam {String} state_type 订单状态 state_new待付款 state_pay待使用 state_success已完成 state_cancel已取消
     * @apiParam {String} query_start_date 开始日期 YYYY-MM-DD
     * @apiParam {String} query_end_date 结束日期 YYYY-MM-DD
     * @apiParam {String} page 页码
     * @apiParam {String} pagesize 每页显示数量
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Object[]} result.order_list 订单列表 （返回字段参考vrorder）
     * @apiSuccess {Boolean} result.order_list.if_cancel  是否可取消 true是false否
     * @apiSuccess {String} result.order_list.goods_image_url  商品图片完整路径
     * @apiSuccess {Int} result.page_total  总页数
     * @apiSuccess {Boolean} result.hasmore  是否有更多 true是false否
     */
    public function order_list() {
        
        $vrorder_model = model('vrorder');

        $condition = array();
        $condition[] = array('store_id','=',$this->store_info['store_id']);
        $order_key = input('post.order_sn');
        if (preg_match('/^\d{10,20}$/', $order_key)) {
            $condition[] = array('order_sn','=',$order_key);
        } elseif ($order_key != '') {
            $condition[] = array('goods_name','like', '%' . $order_key . '%');
        }
        $state_type = input('post.state_type');
        if ($state_type != '') {
            switch($state_type){
                case 'state_new':
                    $condition[] = array('order_state','=',ORDER_STATE_NEW);
                    break;
                case 'state_success':
                    $condition[] = array('order_state','=',ORDER_STATE_SUCCESS);
                    break;
                case 'state_noeval':
                    $condition[] = array('order_state','=',ORDER_STATE_SUCCESS);
                    $condition[] = array('evaluation_state','=',0);
                    break;
                case 'state_cancel':
                    $condition[] = array('order_state','=',ORDER_STATE_CANCEL);
                    break;
            }
        }
        $query_start_date = input('post.query_start_date');
        $query_end_date = input('post.query_end_date');
        $if_start_date = preg_match('/^20\d{2}-\d{2}-\d{2}$/', $query_start_date);
        $if_end_date = preg_match('/^20\d{2}-\d{2}-\d{2}$/', $query_end_date);
        $start_unixtime = $if_start_date ? strtotime($query_start_date) : null;
        $end_unixtime = $if_end_date ? strtotime($query_end_date) : null;
        if ($start_unixtime || $end_unixtime) {
            $condition[] = array('add_time', 'time', array($start_unixtime, $end_unixtime));
        }

        if (input('post.skip_off') == 1) {
            $condition[] = array('order_state','<>',ORDER_STATE_CANCEL);
        }


        $order_list = $vrorder_model->getVrorderList($condition, $this->pagesize, '*', 'order_id desc');
        $mobile_page = $vrorder_model->page_info;
        
        foreach ($order_list as $key => $order) {
            //显示取消订单
            $order_list[$key]['if_cancel'] = $vrorder_model->getVrorderOperateState('store_cancel', $order);

            $order_list[$key]['goods_image_url'] = goods_cthumb($order['goods_image'], 270, $order['store_id']);

            $order_list[$key]['goods_type_cn'] = get_order_goodstype($order['goods_type']);
        }
        $result = array_merge(array('order_list' => $order_list), mobile_page($mobile_page));
        ds_json_encode(10000, '',$result);
    }

    /**
     * @api {POST} api/Sellervrorder/order_cancel 取消虚拟订单
     * @apiVersion 1.0.0
     * @apiGroup Sellervrorder
     *
     * @apiHeader {String} X-DS-KEY 卖家授权token
     *
     * @apiParam {String} order_id 订单id
     * @apiParam {String} reason 理由
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     */
    public function order_cancel() {
        $order_id = intval(input('post.order_id'));
        $reason = input('post.reason');
        $vrorder_model = model('vrorder');
        $logic_vrorder = model('vrorder','logic');
        $condition = array();
        $condition[] = array('order_id','=',$order_id);
        $condition[] = array('store_id','=',$this->store_info['store_id']);
        $order_info = $vrorder_model->getVrorderInfo($condition);

        $if_allow = $vrorder_model->getVrorderOperateState('store_cancel', $order_info);
        if (!$if_allow) {
            return callback(false, '无权操作');
        }

        $result = $logic_vrorder->changeOrderStateCancel($order_info, 'seller',  $reason, true);
        if (!$result['code']) {
            ds_json_encode(10001,$result['msg']);
        }
        ds_json_encode(10000, '',1);
    }
    
 
    /**
     * @api {POST} api/Sellervrorder/order_info 虚拟订单详情
     * @apiVersion 1.0.0
     * @apiGroup Sellervrorder
     *
     * @apiHeader {String} X-DS-KEY 卖家授权token
     *
     * @apiParam {String} order_id 订单id
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据 （返回字段参考vrorder）
     */
    public function order_info()
    {
        $order_id = intval(input('param.order_id'));
        if ($order_id <= 0) {
            ds_json_encode(10001,'订单不存在');
        }
        $vrorder_model = model('vrorder');
        $condition = array();
        $condition[] = array('order_id','=',$order_id);
        $condition[] = array('store_id','=',$this->store_info['store_id']);
        $order_info = $vrorder_model->getVrorderInfo($condition);
        if (empty($order_info) || $order_info['delete_state'] == ORDER_DEL_STATE_DROP) {
            ds_json_encode(10001,'订单不存在');
        }
        $order_list = array();
        $order_list[$order_id] = $order_info;

        //显示取消订单
        $order_info['if_cancel'] = $vrorder_model->getVrorderOperateState('buyer_cancel', $order_info);


        $order_info['goods_image_url'] = goods_cthumb($order_info['goods_image'], 270, $order_info['store_id']);

        $order_info['add_time'] = date('Y-m-d H:i:s', $order_info['add_time']);
        $order_info['payment_time'] = $order_info['payment_time'] ? date('Y-m-d H:i:s', $order_info['payment_time']) : '';
        $order_info['finnshed_time'] = $order_info['finnshed_time'] ? date('Y-m-d H:i:s', $order_info['finnshed_time']) : '';
        
        $order_info['goods_type_cn'] = get_order_goodstype($order_info['goods_type']);

        ds_json_encode(10000, '', array('order_info' => $order_info));
    }
}
?>
